﻿using Common.Device.Enums;
using Common.Device.Models;
using Common.Tool.DataTools;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Common.Device.Views
{
    /// <summary>
    /// ShiBoQiView.xaml 的交互逻辑
    /// </summary>
    public partial class ShiBoQiView : UserControl
    {
        private ViewModels.ShiBoQiViewModel viewModel = new ViewModels.ShiBoQiViewModel();
        public ShiBoQiView(DeviceEnum.DeviceModelEnum model, Order order = null)
        {
            InitializeComponent();
            //根据仪器型号加载界面
            switch (model)
            {
                case DeviceEnum.DeviceModelEnum.DHO1202:
                    viewModel.Channels = new System.Collections.ObjectModel.ObservableCollection<int> { 1, 2 };
                    break;
                default:
                    break;
            }
            DataContext = viewModel;
            if (order != null)
            {
                //根据测试指令刷新界面
                Enum.TryParse(order.temp, out ShiBoQiEnum.TestMode mode);
                ShiBoQi parameter = JsonConvert.DeserializeObject<ShiBoQi>(order.deviceModel);
                switch (mode)
                {
                    case ShiBoQiEnum.TestMode.峰峰值:
                        viewModel.MenuIndex = 0;
                        viewModel.VppChannel = parameter.通道;
                        viewModel.VppVol = parameter.电压挡位;
                        viewModel.VppTime = parameter.时基;
                        viewModel.VppCoup = parameter.耦合方式;
                        viewModel.VppLimit = parameter.带宽限制;
                        viewModel.VppMin = parameter.下限值?.ToString();
                        viewModel.VppMax = parameter.上限值?.ToString();
                        viewModel.WaitTime = parameter?.等待时间 ?? 0;
                        break;
                    default:
                        break;
                }
            }
        }
        //仪器对象
        public Interfaces.IShiBoQi Device;

        private void VppClick(object sender, RoutedEventArgs e)
        {
            //输入确认
            double? min = null; double? max = null;
            if (!DataConvert.GetDouble(viewModel.VppMax, ref max))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.VppMin, ref min))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //创建参数
            Models.ShiBoQi parameter = new Models.ShiBoQi()
            {
                mode = ShiBoQiEnum.TestMode.峰峰值,
                通道 = viewModel.VppChannel,
                电压挡位 = viewModel.VppVol,
                时基 = viewModel.VppTime,
                耦合方式 = viewModel.VppCoup,
                带宽限制 = viewModel.VppLimit,
                等待时间 = viewModel.WaitTime,
                上限值 = max,
                下限值 = min
            };
            if (parameter.MinMaxCheck() == false)
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }
    }
}
